package com.example.wordapp.controller;

import com.example.wordapp.pojo.Result;
import com.example.wordapp.pojo.User;
import com.example.wordapp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public Result list(HttpServletRequest request) {

        log.info("查询所有用户列表");
        List<User> userList = userService.findAll();
        return Result.success(userList);
    }

    @RequestMapping("/login")
    public Result selectUser(HttpServletRequest request, @RequestBody User loginUser) {
        log.info("登录, id: {}, code: {}, name: {}", loginUser.getId(), loginUser.getCode(), loginUser.getName());
        User user = userService.login(loginUser.getId(), loginUser.getCode());
        System.out.println(user);
        if (user == null) {
            return Result.error("用户名和授权码不匹配");
        }
        return Result.success(user);
    }

}
